package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.CouponExchangeBo;
import com.cskaoyan.bean.bo.CouponReceiveBo;
import com.cskaoyan.bean.entity.Coupon;
import com.cskaoyan.bean.entity.User;
import com.cskaoyan.bean.entity.WxCouponInfo;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.controller.wx.service.WxCouponService;
import com.cskaoyan.service.PromotionService;
import com.cskaoyan.utils.CurrentUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  小程序优惠券Controller
 *
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    PromotionService promotionService;

    @Autowired
    WxCouponService wxCouponService;

    @RequestMapping("mylist")
    public BaseRespVo mylist(int status, BaseParam baseParam){
        BasePageData<WxCouponMyListVo> wxCouponMyListVoBasePageData = promotionService.getMyList(status,baseParam);
        return BaseRespVo.ok(wxCouponMyListVoBasePageData);
    }

    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody CouponReceiveBo couponReceiveBo){
        int couponStatus = wxCouponService.receive(couponReceiveBo);
        if(couponStatus == 1000){
            return BaseRespVo.fail("请登录后再领取");
        }
        if(couponStatus == 740){
            return BaseRespVo.fail("超过单人领取上限");
        }
        if(couponStatus == 741){
            return BaseRespVo.fail("优惠券已经领取完");
        }
        if(couponStatus == 742){
            return BaseRespVo.fail("优惠券不存在或不在领取时间");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody CouponExchangeBo couponExchangeBo){
        int couponStatus = wxCouponService.exchange(couponExchangeBo);
        if(couponStatus == 1000){
            return BaseRespVo.fail("请登录后再领取");
        }
        if(couponStatus == 740){
            return BaseRespVo.fail("超过单人领取上限");
        }
        if(couponStatus == 741){
            return BaseRespVo.fail("优惠券已经领取完");
        }
        if(couponStatus == 742){
            return BaseRespVo.fail("优惠券不存在或不在领取时间");
        }
        return BaseRespVo.ok();
    }

    /**
     * auther yangwenjian
     * TODO 选择优惠券
     */
    @GetMapping("selectlist")
//    public BaseRespVo selectlist(Integer cartId, Integer grouponRulesId)
//    {
////        WxselectCouponListVo wxselectCouponListVo = wxCouponService.selectCouponList(userId, cartId, grouponRulesId);
//        BasePageData basePageData = wxCouponService.selectCouponList(cartId, grouponRulesId);
//        return BaseRespVo.ok(basePageData);
//    }
    public BaseRespVo selectlist(Integer cartId, Integer grouponRulesId) {
        User user = (User) SecurityUtils.getSubject().getPrincipals()
                .getPrimaryPrincipal();//获取已登录的用户信息.
        if (user.getId() == null) {
            return BaseRespVo.fail("请登录");
        }

        //WxCouponMyListVo wxCouponMyListVo = wxCouponService.queryMyAvailableCoupon((user.getId()));
        List<WxCouponInfo> WxCouponInfos = wxCouponService.selectCouponList(cartId, grouponRulesId);
        return BaseRespVo.ok(BasePageData.ok(WxCouponInfos));
    }

}
